//
//  RelationSeatViewController.m
//  OrderSeat
//
//  Created by 王威 on 13-12-22.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "RelationSeatViewController.h"
#import "SelectDishViewController.h"
#import "ShopHomeViewController.h"
#import "ConfirmOrderCell.h"

@implementation RelationSeatViewController

#define 初始化函数
- (id)initWithShopId:(NSString*)shopId
{
    self = [super initWithNibClass:[RelationSeatViewController class]];
    if (self) {
        self.shopId = shopId;
        _dataSource = [[RelationSeatDataSource alloc] initWithVC:self];
    }
    return self;
}

#define 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [_dataSource fetchOrderSeats];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

#define 初始化UI
- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"订单关联"];
    [self createRightButton:@"跳过"];
    
    self.seatTable.delegate = _dataSource;
    self.seatTable.dataSource = _dataSource;
    
    [_descriptionLabel drawBorderWithColor:[UIColor redColor]];
    [_descriptionLabel autoHeightForMutiLines];
    _descriptionLabel.height = 60;
    
    _theScrollview.hidden = YES;
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    [self pushOrPopViewController:[ShopHomeViewController class]];
}

- (void)rightButtonClick:(id)sender
{
    SelectDishViewController *selectDishVC = [[SelectDishViewController alloc] initWithShopId:_shopId];
    [self.navigationController pushViewController:selectDishVC animated:YES];
}

#define 提交订单
- (IBAction)nextAction:(id)sender
{
    if (![self checkForm]) return;
    
    NSString *seats = [self getSeatString];
    
    SelectDishViewController *selectDishVC = [[SelectDishViewController alloc] initWithShopId:_shopId];
    selectDishVC.selectSeats = seats;
    [self.navigationController pushViewController:selectDishVC animated:YES];
}

#pragma 获取选中的座位ID列表
- (NSString*)getSeatString
{
    NSMutableArray *selectedSeats = [NSMutableArray array];
    for (NSDictionary *dic in _dataSource.seatList) {
        int selected = [[dic objectForKey:@"selected"] intValue];
        if (selected > 0) {
            [selectedSeats addObject:dic];
        }
    }
    
    int count = 0;
    NSString *string = @"";
    for (NSDictionary *dic in selectedSeats) {
        NSString *orderNo = [dic objectForKey:@"danhao"];
        if (count == _dataSource.seatList.count-1) {
            string = [string stringByAppendingFormat:@"%@", orderNo];
        }else{
            string = [string stringByAppendingFormat:@"%@,", orderNo];
        }
        count++;
    }
    NSLog(@"seat:%@",string);
    return string;
}

#define 验证表单数据是否合法
- (BOOL)checkForm
{
    if ([self isSelectedSeat] == 0) {
        [AppDelegate showMessage:@"请选择关联已预订的座位，不选择请点击右上角“跳过”！"];
        return NO;
    }
    
    /*if ([self isSelectedSeat] > 1) {
        [AppDelegate showMessage:@"只能选择一个座位！"];
        return NO;
    }*/
    
    return YES;
}

#define 是否选中座位
- (int)isSelectedSeat
{
    int selectedCount = 0;
    for(NSDictionary *dic in _dataSource.seatList)
    {
        int selected = [[dic objectForKey:@"selected"] intValue];
        if (selected == 1) {
            selectedCount++;
        }
    }
    return selectedCount;
}

#define 重新布局视图
- (void)reLayoutSubviews
{
    _theScrollview.hidden = NO;
    
    [_seatTable reloadData];
    [_seatTable autoHeight];
    
    [UIView autoLayoutVertical:@[_descriptionLabel, _seatTable, _markLabel, _nextBtn]];
    [_theScrollview autoHeight];
}

@end

//=======================================================================================
#pragma ===================数据源、数据请求等封装============================================
//=======================================================================================

#define SEAT_LIST_TAG 1001
#define ORDER_DISH_TAG 1002
#define CELL_HEIGHT 68

@implementation RelationSeatDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)fetchOrderSeats
{
    NSString *url = [NSString stringWithFormat:API_SEAT_ORDERS, self.viewController.shopId,[AppSetting userName]];
    [self get:url tag:SEAT_LIST_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    if (request.tag == SEAT_LIST_TAG) {
        self.seatList = [json objectForKey:@"zuowei"];
        
        if (_seatList.count > 0) {
            for (NSMutableDictionary *dic in self.seatList) {
                [dic setObject:@"0" forKey:@"selected"];
            }
        }
        [_viewController reLayoutSubviews];
    }
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _seatList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellId = @"ConfirmOrderCell";
    
    ConfirmOrderCell *cell = [_viewController createCellWithNib:CellId tableview:tableView];
    
    NSMutableDictionary *dic = [_seatList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    
    return cell;
}

@end




